package com.woniuxy.aopdemo1;


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * @Author: 马宇航
 * @Todo: 切面类：切入点和通知！
 * @DateTime: 22/08/22/0022 16:56
 * @Component: 成都蜗牛学苑
 **/
public class MyAspect implements MethodInterceptor {
    //定义通知: 前置通知
    public void checkPermission(){
        System.out.println("开启权限检查，如果有权限就放行……");
    }
    //定义后置通知
    public void commitThransaction(){
        System.out.println("模拟事务提交！");
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        checkPermission();
        if (invocation.getMethod().getName().startsWith("add")) {
            System.out.println("是新增方法！");
        }
        //核心还是用的反射
        Object proceed = invocation.proceed();
        commitThransaction();
        return proceed;
    }
}
